package ywh.list
{
	import ywh.manager.Tag;
	import ywh.utils.XMLUtils;
	
	/**
	 * 课程数据类;
	 *
	 * @author 杨伟华
	 * @version 2.0
	 *
	 * Created 1/21/2013 12:40 AM
	 */
	
	public class CourseItem
	{
		private var _xmlData:XML;
		private var _defaultData:XML;
		private var _playerId:String;
		private var _startTime:Number;
		private var _endTime:Number;
		private var _duration:Number;
		
		public function CourseItem()
		{
		}
		
		/**
		 * 填充数据
		 * @param	xmlData
		 */
		public function set data(xmlData:XML):void
		{
			if (xmlData)
			{
				_xmlData = xmlData;
				_startTime = getPropNumber(Tag._START_TIME);
			}
			else
			{
				clearData();
			}
		}
		
		public function get data():XML
		{
			return _xmlData;
		}
		
		/**
		 * 设置默认填充数据;
		 */
		public function set defaultData(value:XML):void
		{
			_defaultData = value;
		}
		
		/**
		 * 清除数据
		 */
		private function clearData():void
		{
			_xmlData = null;
			_playerId = null;
			_duration = NaN;
			_startTime = NaN;
			_endTime = NaN;
		}
		
		/**
		 * 获得起始时间;
		 */
		public function get startTime():Number
		{
			return _startTime;
		}
		
		public function set startTime(value:Number):void
		{
			_startTime = value;
			setPropValue(Tag._START_TIME, String(value));
		}
		
		/**
		 * 结束时间;
		 */
		public function get endTime():Number
		{
			return _endTime;
		}
		
		public function set endTime(value:Number):void
		{
			_endTime = value;
			_duration = endTime - startTime;
		}
		
		/**
		 * 获得时长；
		 */
		public function get duration():Number
		{
			return _duration;
		}
		
		public function get playerId():String
		{
			return _playerId ||= getPropString(Tag._PLAYER_ID);
		}
		
		/**
		 * 获取文本信息;
		 * @param	infoName
		 * @return
		 */
		public function getPropString(infoName:String):String
		{
			var privateProp:String = XMLUtils.getPropString(_xmlData, infoName);
			return (privateProp || XMLUtils.getPropString(_defaultData, infoName));
		}
		
		/**
		 * 获取数字信息;
		 * @param	infoName
		 * @return
		 */
		public function getPropNumber(infoName:String):Number
		{
			var privateProp:Number = XMLUtils.getPropNumber(_xmlData, infoName);
			return isNaN(privateProp) ? XMLUtils.getPropNumber(_defaultData, infoName) : privateProp;
		}
		
		/**
		 * 获取布尔信息;
		 * @param	infoName
		 * @return
		 */
		public function getPropBool(infoName:String):Boolean
		{
			return getPropString(infoName) == XMLUtils.TRUE_VALUE;
		}
		
		/**
		 * 修改或添加节点属性;
		 * @param	prop
		 * @param	value
		 */
		public function setPropValue(prop:String, value:String):void
		{
			_xmlData[prop] = value;
		}
	
	}

}